@php
    $user = Auth::user();
    $role = $user->roles()->get();
	$role_id = $user->cur_role_id;
@endphp
@extends('layouts.default')

@section('title', '')

@push('scripts')
<script>
    layui.use(['layer', 'element','form','table','laypage','laytpl','laydate'], function(){
        var layer = layui.layer;
        var form = layui.form;
        var element = layui.element;
        var table = layui.table;
        var laypage = layui.laypage;
        var laytpl = layui.laytpl;
        var laydate = layui.laydate;
        var param = {}; 
    
     
        getList(1);
        renderSelect();

        //日期时间范围
        laydate.render({
            elem: '#date'
            ,type: 'date'
            ,range: true
        });
      
        //下拉框初始化
        function renderSelect(){
            getDictionaryItem({'diccode': ['award_type','LEVEL','award_grade']}, function (res){
                if(res.code === 1) {  
                    let data = res.data;
                    for(var k in data) {
                        if(k == 'hdjb'){   
                            $('[data-role='+k+'List]').empty().append('<option value="">请选择</option>');
                        }else{
                            $('[data-role='+k+'List]').empty().append('<option value="">请选择</option>');
                        }
                        let d = data[k];
                        for (var i in d) {
                            $('[data-role='+k+'List]').append('<option value="' + d[i].optionId + '">' + d[i].selectValue + '</option>');
                        }
                    }
                    form.render(); 
                }
            });  
            getTermData({},function(res){
                if(res.code == 1){
                    $('select[name=data_version_id]').empty();
                    $('select[name=data_version_id]').append('<option value="">请选择</option>');
                    for(var i in res.data){
                        $('select[name=data_version_id]').append('<option value="'+res.data[i].id+'">'+res.data[i].title+'</option>');
                    }
                    form.render(); 
                }
            })   
        }

        // 查询
        form.on('submit(queryBtn)', function(data) {
            Object.assign(param, {
                    "search": data.field
            });
            getList(1);
            return false; 
        });

        // 导出
        form.on('submit(exportBtn)', function(data) {
            Object.assign(param, {
                    "search": data.field
            });
            exportJob(param,function(res){
                if(res.code == '1'){
                    exportsDel('岗位竞聘.xlsx','/download/excel/'+res.data.file_name);
                }else{
                    layer.msg(res.msg, {icon: 5,shift : 6});
                }
            });
            return false; 
        });

        //导出excel
        function exportsDel(showName,fileName){
            window.location.href = "{{route('download.FileDownloadDel')}}"+'?filename='+showName+'&file_url='+fileName;
        }
       
        //重置
        $('button[type=reset]').click(function(){
            $('#search-form')[0].reset();
            $('button[lay-filter=queryBtn]').click(); 
            return false;    
        })

        function getList(page){
            Object.assign(param,{"page":page});		        		        	
            getJobList(param,function(res){		
                if(res.code == 1){
                    table.render({
                        elem: '#tablelist'
                        ,toolbar: '#toolbarDemo'
                        ,defaultToolbar:[]
                        ,data: res.data.data
                        ,limit:res.data.limit
                        ,skin:'nob'
                        ,even:true
                        //,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                        ,cols: [[ 
                        {field: '', title: '学年学期',width:'10%',align:'center',templet: function(d) {
                            // return (page-1)*res.data.limit+d.LAY_INDEX;
                            if(d.data_version_name === null){
                                return '';
                            }else{
                                return d.data_version_name;
                            }                        
                        }}							                  							
                        ,{field: 'name', title: '名称',width:'15%',align:'center'}
                        @if(!is_checker())
                            ,{field: '', title: '考核起止时间',width:'15%',align:'center',templet: function(d) {
                                if(d.check_start !== null && d.check_end !== null){
                                    return d.check_start+' - '+d.check_end; 
                                }else{
                                    return '';
                                }                                                 
                            }}
                            ,{field: '', title: '申报起止时间',width:'15%',align:'center',templet: function(d) {
                                if(d.submit_start !== null && d.submit_end !== null){
                                    return d.submit_start+' - '+d.submit_end; 
                                }else{
                                    return '';
                                }                                                 
                            }}
                            ,{field: 'submit_text', title: '状态',width:'10%',align:'center'}	
                            ,{field: 'apply_text', title: '申报状态',width:'10%',align:'center'}	
                        @else
                            ,{field: 'level_text', title: '岗位级别',width:'10%',align:'center'}																          
                            ,{field: 'sum', title: '岗位空缺数量',width:'7%',align:'center'}	
                            ,{field: '', title: '考核起止时间',width:'13%',align:'center',templet: function(d) {
                                if(d.check_start !== null && d.check_end !== null){
                                    return d.check_start+' - '+d.check_end; 
                                }else{
                                    return '';
                                }                                                 
                            }}																
                            ,{field: 'submit_text', title: '状态',width:'7%',align:'center'}																
                            ,{field: 'author_name', title: '创建人',width:'7%',align:'center'}														
                            ,{field: 'created_at', title: '创建时间',width:'10%',align:'center'}	
                        @endif
                        													
                        ,{field: '', title: '操作',toolbar: '#barDemo',width:'',align:'center'}
                        ]]
                    });

                    $('#pageBar').empty();                     
                    if(res.data.total >0){
                        //分页
                        laypage.render({
                        elem: 'pageBar' //注意，这里的 test1 是 ID，不用加 # 号
                        ,count: [res.data.total] //数据总数，从服务端得到
                        ,curr: page
                        ,layout: ['limit', 'count', 'prev', 'page', 'next',  'skip']                               
                        ,limit:res.data.limit
                        ,jump: function(obj, first) {
                                //首次不执行
                                if (!first) {
                                    Object.assign(param,{"limit":obj.limit});                                
                                    getList(obj.curr)
                                }
                            }
                        });
                    } 
                     setBtnPermission();                           
                }else {
                    layer.msg(res.msg, {icon: 5});
                } 
                                    
            })
        }



        table.on('tool(tablelist)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
     
            if(layEvent === 'del'){ //删除
                layer.confirm('确定删除吗?', function(index){
                    delJobApply({'id':data.id},function(res){
                        if(res.code == '1'){
                            layer.msg(res.msg,{icon: 6,time: 1000});
                            obj.del(); 
                            layer.close(index);
                        }else{
                            layer.msg(res.msg, {icon: 5,shift : 6});
                        }
                    })                             
                });
            }else if(layEvent === 'edit'){     
              window.location.href = "{{route('rating.job.add')}}?id="+data.id;
     
            }else if(layEvent === 'apply'){            
                window.location.href = "{{route('rating.job.apply')}}?id="+data.id+'&scale_id='+data.scale_id+'&materials_date='+JSON.parse(data.materials_date);              
            }else if(layEvent === 'see'){
                window.location.href = "{{route('rating.job.see')}}?id="+data.id+'&scale_id='+data.scale_id;
            }else if(layEvent === 'adminsee'){
                window.location.href = "{{route('rating.job.adminsee')}}?id="+data.id+'&scale_id='+data.scale_id;
            }
        });
              

    })
</script>


<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn layui-btn-primary layui-border-blue layui-btn-xs" lay-event="edit"  btnPermission="202">编辑</a>
    @{{# if(d.is_apply == 1){ }}
    <a class="layui-btn layui-btn layui-btn-primary layui-border-blue layui-btn-xs" lay-event="see"  btnPermission="198">查看</a>
    @{{# }else{ }}
    <a class="layui-btn layui-btn layui-btn-primary layui-border-blue layui-btn-xs layui-btn-disabled" lay-event=""  btnPermission="198">查看</a>
    @{{# } }} 

    @{{# if(d.is_check == 1){ }}
    <a class="layui-btn layui-btn layui-btn-primary layui-border-blue layui-btn-xs"  lay-event="adminsee"  >查看</a>
    @{{# } }}

    @{{# if(d.submit_text == '进行中' && d.is_apply == 1){ }} 
    <a class="layui-btn layui-btn layui-btn-primary layui-border-blue layui-btn-xs" lay-event="apply"  btnPermission="199">申报</a>
    @{{# }else{ }}  
    <a class="layui-btn layui-btn layui-btn-primary layui-border-blue layui-btn-xs layui-btn-disabled" lay-event=""  btnPermission="199">申报</a>
    @{{# } }}
    
    
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="del" btnPermission="200">删除</a>
</script>
<!---头部工具栏--->
<script type="text/html" id="toolbarDemo">
    <div class="list-head">
            <h3>岗位竞聘</h3>
            <div class="layui-btn-container list-opt-btns">
                <a btnPermission="197" class="layui-btn layui-btn-normal" href="{{route('rating.job.add')}}" id="addBtn"> + 新增 </a>
            </div>
        </div>
</script>

@endpush

@section('content')
    <style>
         /* .layui-table-body{
            overflow: hidden;
        }  */
    </style>
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-row">    
                <form class="layui-form" id="search-form" >   
                    <div class="layui-form-item">             
                        <div class="layui-inline">
                            <label class="layui-form-label" >学年学期:</label>
                            <div class="layui-input-inline">                  
                                <select name="data_version_id" class="layui-select" lay-filter="term_id" data-role="term_id" style="width:190px;"></select>                 
                            </div>
                        </div>
                        @if(is_checker())
                        <div class="layui-inline">
                            <label class="layui-form-label" >创建人:</label>
                            <div class="layui-input-inline">                  
                                <input autocomplete="off" type="text" name="author_name" class="layui-input" placeholder="请输入">                
                            </div>
                        </div>
                        @endif
                        <div class="layui-inline">
                            <label class="layui-form-label" >名称:</label>
                            <div class="layui-input-inline">                  
                                <input autocomplete="off" type="text" name="name" class="layui-input" placeholder="请输入">                
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label" >岗位级别:</label>
                            <div class="layui-input-inline">                  
                                <input autocomplete="off" type="text" name="level" class="layui-input" placeholder="请输入">                
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label" style="width: 90px;" >考核起止时间:</label>
                            <div class="layui-input-inline">                  
                                <input id="date"  autocomplete="off" type="text" name="date" class="layui-input" placeholder="请输入">                
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label" >状态:</label>
                            <div class="layui-input-inline">                  
                                <select data-role="" name="status"   lay-filter="" >
                                    <option value="">请选择</option>
                                    <option value="0">待提交</option>
                                    <option value="1">未开始</option>
                                    <option value="2">进行中</option>
                                    <option value="3">已结束</option>
                                </select>                 
                            </div>
                        </div>

                        <div class="layui-inline" > 
                                <button lay-submit id="queryBtn" class="pear-btn pear-btn-md pear-btn-primary" lay-filter="queryBtn" >
                                    查询
                                </button>
                                <button type="reset" class="pear-btn pear-btn-md reset">
                                    重置
                                </button>
                                <button lay-submit id="exportBtn" class="pear-btn pear-btn-md pear-btn-primary" lay-filter="exportBtn" btnPermission="336">
                                    导出
                                </button>
                        </div>
                    </div>

                 
                </form>      
            </div>
        </div>
    </div>


    <div class="layui-card">

        <div class="layui-card-body">
            <table class="layui-table" id="tablelist" lay-filter="tablelist" ></table>
            <div class="pagebarbox" style="text-align: end"><div id="pageBar" class="pagebar"></div></div>
        </div>
    </div>
  
   
@endsection
